package com.sgcc.activiti.service.impl;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.sgcc.activiti.domain.OAnomalyInfo;
import com.sgcc.activiti.domain.OAnomalyTypeDuration;
import com.sgcc.activiti.domain.OIndexMaintain;
import com.sgcc.activiti.mapper.OAnomalyInfoMapper;
import com.sgcc.activiti.mapper.OAnomalyTypeDurationMapper;
import com.sgcc.activiti.service.IOAnomalyInfoService;
import com.sgcc.activiti.service.IOIndexMaintainService;
import com.sgcc.common.core.domain.R;
import com.sgcc.common.core.utils.DateUtils;
import com.sgcc.system.api.RemoteDictDataService;
import com.sgcc.system.api.domain.SysDictData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sgcc.activiti.mapper.OSupplierEvaluationMapper;
import com.sgcc.activiti.domain.OSupplierEvaluation;
import com.sgcc.activiti.service.IOSupplierEvaluationService;

/**
 * OSupplierEvaluationService业务层处理
 * 
 * @author sgcc
 * @date 2021-04-09
 */
@Service
public class OSupplierEvaluationServiceImpl implements IOSupplierEvaluationService 
{
    @Autowired
    private OSupplierEvaluationMapper oSupplierEvaluationMapper;

    @Autowired
    private RemoteDictDataService remoteDictDataService;

    @Autowired
    private OAnomalyInfoMapper oAnomalyInfoMapper;

    @Autowired
    private OAnomalyTypeDurationMapper oAnomalyTypeDurationMapper;

    @Autowired
    private IOIndexMaintainService ioIndexMaintainService;

    private Logger logger = LoggerFactory.getLogger(OSupplierEvaluationServiceImpl.class);

    /**
     * 查询OSupplierEvaluation
     * 
     * @param id OSupplierEvaluationID
     * @return OSupplierEvaluation
     */
    @Override
    public OSupplierEvaluation selectOSupplierEvaluationById(Integer id)
    {
        return oSupplierEvaluationMapper.selectOSupplierEvaluationById(id);
    }

    /**
     * 查询OSupplierEvaluation列表
     * 
     * @param oSupplierEvaluation OSupplierEvaluation
     * @return OSupplierEvaluation
     */
    @Override
    public List<OSupplierEvaluation> selectOSupplierEvaluationList(OSupplierEvaluation oSupplierEvaluation)
    {
        if (null != oSupplierEvaluation){
            if (null == oSupplierEvaluation.getType()){
                oSupplierEvaluation.setType("1");
            }
        }else {
            oSupplierEvaluation = new OSupplierEvaluation();
            oSupplierEvaluation.setType("1");
        }
        List<OSupplierEvaluation> oSupplierEvaluations = oSupplierEvaluationMapper.selectOSupplierEvaluationList(oSupplierEvaluation);
        int count = 1;
        for (OSupplierEvaluation oSupplierEvaluation1 : oSupplierEvaluations){
            oSupplierEvaluation1.setRank(count);
            count++;
        }
        return oSupplierEvaluations;
    }

    /**
     * 新增OSupplierEvaluation
     * 
     * @param oSupplierEvaluation OSupplierEvaluation
     * @return 结果
     */
    @Override
    public int insertOSupplierEvaluation(OSupplierEvaluation oSupplierEvaluation)
    {
        return oSupplierEvaluationMapper.insertOSupplierEvaluation(oSupplierEvaluation);
    }

    /**
     * 修改OSupplierEvaluation
     * 
     * @param oSupplierEvaluation OSupplierEvaluation
     * @return 结果
     */
    @Override
    public int updateOSupplierEvaluation(OSupplierEvaluation oSupplierEvaluation)
    {
        return oSupplierEvaluationMapper.updateOSupplierEvaluation(oSupplierEvaluation);
    }

    /**
     * 批量删除OSupplierEvaluation
     * 
     * @param ids 需要删除的OSupplierEvaluationID
     * @return 结果
     */
    @Override
    public int deleteOSupplierEvaluationByIds(Integer[] ids)
    {
        return oSupplierEvaluationMapper.deleteOSupplierEvaluationByIds(ids);
    }

    /**
     * 删除OSupplierEvaluation信息
     * 
     * @param id OSupplierEvaluationID
     * @return 结果
     */
    @Override
    public int deleteOSupplierEvaluationById(Integer id)
    {
        return oSupplierEvaluationMapper.deleteOSupplierEvaluationById(id);
    }

    @Override
    public void execute() {
        // 获取上个月的开始时间和结束时间
        Date beginDayOfLastMonth = DateUtils.getBeginDayOfLastMonth();
        String startDate = beginDayOfLastMonth.toString();
        String starttime = startDate.substring(0, 19);
        Date endDayOfLastMonth = DateUtils.getEndDayOfLastMonth();
        String endDate = endDayOfLastMonth.toString();
        String endtime = endDate.substring(0, 19);
        List<OSupplierEvaluation> oSupplierEvaluationList = new ArrayList<>();



        // 查询上个月各个供应商的异常时长最长的时长
        List<OAnomalyInfo> oAnomalyInfoList =  oAnomalyInfoMapper.selectOAnomalyInfoduration(starttime,endtime);
        // 获取 指标为异常时长 的打分规则
        List<OAnomalyTypeDuration> oAnomalyTypeDurationList = getoAnomalyTypeDurations("异常时长");
        // 查询异常时长指标说明
        OIndexMaintain oIndexMaintain = new OIndexMaintain();
        oIndexMaintain.setIndexName("异常时长");
        List<OIndexMaintain> oIndexMaintains = ioIndexMaintainService.selectOIndexMaintainList(oIndexMaintain);

        if (oIndexMaintains.size() > 0 && null != oIndexMaintains.get(0)){
            for (OAnomalyInfo anomalyInfo : oAnomalyInfoList){
                OSupplierEvaluation oSupplierEvaluation = new OSupplierEvaluation();
                double duration = anomalyInfo.getDuration().doubleValue();
                for (OAnomalyTypeDuration oAnomalyTypeDuration1:oAnomalyTypeDurationList){
                    if (duration >= oAnomalyTypeDuration1.getDuration() && duration <oAnomalyTypeDuration1.getDurationNext()){
                        double v = oAnomalyTypeDuration1.getScore() * oIndexMaintains.get(0).getIndexWeight();
                        oSupplierEvaluation.setEvaluationScore(v);
                        oSupplierEvaluation.setSystemName(anomalyInfo.getMonSysCode());
                        oSupplierEvaluationList.add(oSupplierEvaluation);
                    }
                }
            }
        }

        List<OSupplierEvaluation> lcsxs = new ArrayList<>();
        // 查询流程时限的打分规则
        List<OAnomalyTypeDuration> oAnomalyTypeDurationlcsx = getoAnomalyTypeDurations("流程时限");
        // 查询流程时限指标说明
        oIndexMaintain.setIndexName("流程时限");
        List<OIndexMaintain> oIndexMaintainList = ioIndexMaintainService.selectOIndexMaintainList(oIndexMaintain);
        if (oIndexMaintainList.size() > 0 && null != oIndexMaintainList.get(0)){
            if (oAnomalyTypeDurationlcsx.size() > 0 && oAnomalyTypeDurationlcsx.get(0) != null){
                // 统计 查询上个月各个供应商的流程时限超过24小时处理的次数
                List<OAnomalyInfo> lcsxList =  oAnomalyInfoMapper.selectOAnomalyInfolcsx(starttime,endtime,oAnomalyTypeDurationlcsx.get(0).getDuration());
                for (OAnomalyInfo anomalyInfo : lcsxList){
                    OSupplierEvaluation oSupplierEvaluation = new OSupplierEvaluation();
                    Integer showCount = anomalyInfo.getShowCount();
                    // 判断如果出现的次数超过10次，则直接分数为0，如果没有超过，则超过几次分数减几
                    if (showCount > 10.0){
                        oSupplierEvaluation.setEvaluationScore(0.0);
                        oSupplierEvaluation.setSystemName(anomalyInfo.getMonSysCode());
                    }else {
                        double v = (oAnomalyTypeDurationlcsx.get(0).getScore() - showCount) * oIndexMaintainList.get(0).getIndexWeight();
                        oSupplierEvaluation.setEvaluationScore(v);
                        oSupplierEvaluation.setSystemName(anomalyInfo.getMonSysCode());
                    }
                    lcsxs.add(oSupplierEvaluation);
                }
            }
        }

        List<OSupplierEvaluation> yccss = new ArrayList<>();
        //统计 查询上个月各个供应商的异常次数
        List<OAnomalyInfo> oAnomalyInfoyccs =  oAnomalyInfoMapper.selectOAnomalyInfoyccs(starttime,endtime);

        // 查询流程时限的打分规则
        List<OAnomalyTypeDuration> yccsList = getoAnomalyTypeDurations("异常次数");
        // 查询流程时限指标说明
        oIndexMaintain.setIndexName("异常次数");
        List<OIndexMaintain> oIndexMaintainyccs = ioIndexMaintainService.selectOIndexMaintainList(oIndexMaintain);
        if (oIndexMaintainyccs.size() > 0 && null != oIndexMaintainyccs.get(0)){
            for (OAnomalyInfo anomalyInfo : oAnomalyInfoyccs){
                OSupplierEvaluation oSupplierEvaluation = new OSupplierEvaluation();
                Integer showCount = anomalyInfo.getShowCount();
                if (0 == showCount){
                    oSupplierEvaluation.setEvaluationScore(10.0);
                }else {
                    for (OAnomalyTypeDuration oAnomalyTypeDuration : yccsList){
                        if (showCount >= oAnomalyTypeDuration.getDuration() && showCount < oAnomalyTypeDuration.getDurationNext()){
                            double v = oAnomalyTypeDuration.getScore() * oIndexMaintainyccs.get(0).getIndexWeight();
                            oSupplierEvaluation.setEvaluationScore(v);
                            oSupplierEvaluation.setSystemName(anomalyInfo.getMonSysCode());
                            yccss.add(oSupplierEvaluation);
                        }
                    }
                }
            }
        }
        List<OSupplierEvaluation> sumList = new ArrayList<>();
        logger.info(new Date()+":计算各个供应商的异常得分开始");
        for (OSupplierEvaluation supplierEvaluation : oSupplierEvaluationList){
            OSupplierEvaluation os = new OSupplierEvaluation();
            for (OSupplierEvaluation lcsx : lcsxs){
                if (supplierEvaluation.getSystemName().equals(lcsx.getSystemName())){
                    double v = supplierEvaluation.getEvaluationScore() + lcsx.getEvaluationScore();
                    os.setSystemName(supplierEvaluation.getSystemName());
                    os.setEvaluationScore(v);
                }
            }
            for (OSupplierEvaluation yccs : yccss){
                if (supplierEvaluation.getSystemName().equals(yccs.getSystemName())){
                    if (os != null && os.getEvaluationScore() != null){
                        double v = os.getEvaluationScore() + yccs.getEvaluationScore();
                        os.setEvaluationScore(v);
                        os.setSystemName(yccs.getSystemName());
                    }else {
                        double v = supplierEvaluation.getEvaluationScore() + yccs.getEvaluationScore();
                        os.setEvaluationScore(v);
                        os.setSystemName(yccs.getSystemName());
                    }
                }
            }
            sumList.add(os);
        }
        logger.info(new Date()+":计算各个供应商的异常得分结束");
        for (OSupplierEvaluation oSupplierEvaluation : sumList){
            oSupplierEvaluation.setStatTime(endtime);
            oSupplierEvaluationMapper.insertOSupplierEvaluation(oSupplierEvaluation);
        }
        logger.info(DateUtils.getLastMonth()+"的统计供应商的异常情况完成");

    }

    private List<OAnomalyTypeDuration> getoAnomalyTypeDurations(String indexName) {
        OIndexMaintain oIndexMaintain = new OIndexMaintain();
        oIndexMaintain.setIndexName(indexName);
        List<OIndexMaintain> oIndexMaintains = ioIndexMaintainService.selectOIndexMaintainList(oIndexMaintain);
        OAnomalyTypeDuration oAnomalyTypeDuration = new OAnomalyTypeDuration();
        if (oIndexMaintains.size() > 0 && null != oIndexMaintains.get(0)){
            oAnomalyTypeDuration.setIndexId(oIndexMaintains.get(0).getId());
        }
        return oAnomalyTypeDurationMapper.selectOAnomalyTypeDurationByIndexId(oAnomalyTypeDuration);
    }
}
